var ColFix = Ext.extend(Ext.Panel, {
	border : false,
	layout : 'border',
	initComponent : function() {
		this.initTables();
		this.initColFix();
		this.items = [this.tables, this.columns];
		ColFix.superclass.initComponent.call(this);
	},
	initTables : function() {
		var me = this;
		this.tables = new Ext.grid.GridPanel({
			region : 'west',
			width : 300,
			split : true,
			loadMask : true,
			viewConfig : {
				forceFit : true
			},
			bodyStyle : 'border-left:0;border-bottom:0;border-top:0;',
			store : new Ext.data.JsonStore({
						fields : ['tablename', 'chinesename'],
						url : My.url('/system/common/queryForList.do'),
						baseParams : {
							table : 't_tables',
							where : 'order by tablename',
							cols : '*'
						},
						root : 'data',
						autoLoad : true
					}),
			sm : new Ext.grid.RowSelectionModel({
				listeners : {
					rowselect : function(t, i, r) {
						// 加载菜单
						var rec = me.tables.getStore().getAt(i);
						var tablename = rec.data.tablename;
						var sql1 = "select *\n"
								+ "  from t_columns\n"
								+ " where tablename || '.' || columnname not in (select tablename || '.' || columnname from t_col_fix) ";
						var sql2 = "select t.* from t_columns t join t_col_fix b on t.tablename=b.tablename and t.columnname=b.columnname ";
						var store1 = me.columns.getStore(0);
						store1.baseParams.sql = sql1 + " and tablename='"
								+ tablename + "' order by columnname";
						store1.load();
						var store2 = me.columns.getStore(1);
						store2.baseParams.sql = sql2 + " where t.tablename='"
								+ tablename + "' order by b.priority";
						store2.load();
						me.columns.tablename = tablename;
					}
				}
			}),
			cm : new Ext.grid.ColumnModel({
						columns : [{
									dataIndex : 'tablename',
									header : '表名',
									width : 300,
									renderer : function(v, m, r) {
										return v + '（' + r.data.chinesename
												+ '）';
									}
								}]
					})
		});
	},
	initColFix : function() {
		var me = this;
		var is = new Ext.ux.ItemSelector({
			imagePath : My.url('/common/extjs/ux/images/'),
			multiselects : [{
				legend : '待选择字段',
				width : 300,
				height : 500,
				store : new Ext.data.JsonStore({
					fields : ['tablename', 'columnname', {
								name : 'chinesename',
								convert : function(v, r) {
									return r.columnname + '（' + v + '）';
								}
							}],
					url : My.url('/system/common/queryBySql.do'),
					baseParams : {
						sql : "select t.* from t_columns t join t_col_fix b on t.tablename=b.tablename and t.columnname=b.columnname where t.tablename='ELE_BUDGET_CATEGORY' order by b.priority"
					},
					root : 'data'
				}),
				displayField : 'chinesename',
				valueField : 'columnname'
			}, {
				legend : '已选择字段',
				width : 300,
				height : 500,
				store : new Ext.data.JsonStore({
							fields : ['tablename', 'columnname', {
										name : 'chinesename',
										convert : function(v, r) {
											return r.columnname + '（' + v + '）';
										}
									}],
							url : My.url('/system/common/queryBySql.do'),
							baseParams : {},
							root : 'data'
						}),
				displayField : 'chinesename',
				valueField : 'columnname'
			}]
		});
		this.columns = new Ext.Panel({
					title : '请将要修改的字段从带选择字段拖到已选择字段下',
					autoScroll : true,
					region : 'center',
					padding : '3px 5px 1px 5px',
					bodyStyle : 'border-bottom:0;border-right:0;',
					headerStyle : 'border-top:0;border-right:0;',
					items : [is],
					getStore : function(index) {
						return is.multiselects[index].store;
					},
					tbar : [{
								text : '保存',
								iconCls : 'icon-save',
								handler : function() {
									me.save();
								}
							}]
				});
	},
	save : function() {
		var me = this;
		var rec = this.tables.getSelectionModel().getSelected();
		if (!rec) {
			TopMy.alert('请选择要操作的表！');
			return;
		}
		var tablename = rec.data.tablename;
		var values = me.columns.getStore(1).getRange();
		var sqls = [];
		Ext.each(values, function(val, i) {
			var sql = "insert into t_col_fix(TABLENAME,COLUMNNAME,PRIORITY) values('"
					+ tablename + "','" + val.data.columnname + "'," + i + ")";
			sqls.push(sql);
		});
		var sql = sqls.length > 0 ? sqls.join(';') + ';' : '';
		TopMy.request({
					url : My.url('/system/common/exec.do'),
					params : {
						sql : "begin delete t_col_fix where tablename='"
								+ tablename + "'; " + sql + " end;"
					}
				});
	}
});
Ext.onReady(function() {
			new Ext.Viewport({
						layout : 'fit',
						items : [new ColFix()]
					});
		});